
*local p = `1'
*global data "/cluster/work/users/ingahe"

*global data "/Users/inga/Dropbox (UiO)/Ais project/inga/data"
*capture log close
*log using "$data/logs/log`p'_hrs", replace



clear all
global main "/Users/inga/Dropbox/Ais project/Repository"
cd "$main/2_fastest_routes"

global data "$main/data_intermediate"

local p = 1
use "$data/input514/trips_hrs_co`p'", clear

forvalues l=1(1)15 {

*local l=1
local k= `l'-1

rename D_* D`k'_*
rename A_a D_acid
rename A_t D_Ttime
sort  D_a D_Tt 
levelsof D_a, local(Da)

foreach i in `Da' {
*local i =19
di "`k'_`i'"
joinby D_a D_T using "$data/input514/trips_Thrs_co`i'", update unmatched(master) 
drop _m

*keep only non-dominated routes 


sort D0_a A_a D0_t A_t
bysort D0_a A_a: g n=_n
sum n

rangestat (min) A_time, int(n 0 .) by(D0_a A_a)
sum A_time_min

drop n
sort D0_a A_a A_time_min D0_t A_time
bysort D0_a A_a A_time_min: drop if dwt[_n]<=dwt[_N] & A_time[_n]>=A_time[_N] & _n!=_N // drop cases where the minimum arrival time also comes with a higher draught


bysort D_a A_a A_time_min: g nn=_n
g dropme=.
sum nn
global N = r(max)
forvalues s=2(1)$N {
	local r = `s'-1
	di `r'
	sort D0_a A_a A_time_min D0_t A_time
	bysort D0_a A_a A_time_min: replace dropme=1 if dwt[_n]<=dwt[_n+`r'] & A_time[_n]>=A_time[_n+`r']  // find cases that are not dominated by the minimum-arrival-time connection, but by some other in between.
}

drop if dropme==1
drop dropme A_time_min nn


/*

quietly g tt=A_t - D0_t 
quietly egen mintt=min(tt) if A_a!=., by(D0_t A_a)
quietly  drop if tt!=mintt & A_a!=.
drop mintt

*keep only fastest for given arrival time
quietly egen mintt=min(tt) if A_a!=., by(A_t A_a)
quietly  drop if tt!=mintt & A_a!=.
drop tt mintt 

}*/


*replace missings with last port data
replace A_acid = D_ac if A_acid==.
replace A_t = D_Tt if A_t==.

*drop intermediate times
drop D_T

*drop return trips
forvalues j=0(1)`k' {
drop if A_acid==D`j'_acid & A_acid!=D_acid
}

replace D_a = A_a if D_a==D`k'_a


local m=`k'-1
local n=`k'-2

if `m'>=1 {
replace D`k'_a = D_a if D`k'_a==D`m'_a
}


while `m'>=1 {
local o = `m'+1
replace D`m'_a=D`o'_a if D`m'_a==D`n'_a
local m = `m'-1
local n = `n'-1
}

duplicates drop

*redo dropping dominated routes for the ones that had no match in the last round
sort D0_a A_a D0_t A_t
bysort D0_a A_a: g n=_n
sum n

rangestat (min) A_time, int(n 0 .) by(D0_a A_a)
sum A_time_min

drop n
sort D0_a A_a A_time_min D0_t A_time
bysort D0_a A_a A_time_min: drop if dwt[_n]<=dwt[_N] & A_time[_n]>=A_time[_N] & _n!=_N // drop cases where the minimum arrival time also comes with a higher draught


bysort D_a A_a A_time_min: g nn=_n
g dropme=.
sum nn
global N = r(max)
forvalues s=2(1)$N {
	local r = `s'-1
	di `r'
	sort D0_a A_a A_time_min D0_t A_time
	bysort D0_a A_a A_time_min: replace dropme=1 if dwt[_n]<=dwt[_n+`r'] & A_time[_n]>=A_time[_n+`r']  // find cases that are not dominated by the minimum-arrival-time connection, but by some other in between.
}

drop if dropme==1
drop dropme A_time_min nn


* drop routes with more stops
egen nhops = rownvals(*acid)
egen minhops=min(nhops), by(D0_ac D0_t A_aci A_t dwt)
keep if nhops==minho
drop minhops nhops
}

save "$data/output/hrs`p'_co", replace

capture log close


